package com.github.shiqiyue.learn.curator;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.ChildData;
import org.apache.curator.framework.recipes.cache.TreeCache;
import org.apache.curator.framework.recipes.cache.TreeCacheEvent;
import org.apache.curator.framework.recipes.cache.TreeCacheListener;

/***
 * treeCache测试
 * @author wenyao.wu
 * @date 2019/1/21
 */
public class TreeCacheTest {

    public static void main(String[] args) throws Exception {
        CuratorFramework client = ClientUtil.getClient(null);

        TreeCache watcher = TreeCache.newBuilder(client, "/t1").setCacheData(true).setMaxDepth(2).build();
        watcher.getListenable().addListener(new TreeCacheListener() {
            @Override
            public void childEvent(CuratorFramework client1, TreeCacheEvent event) {
                ChildData data = event.getData();
                if (data == null) {
                    System.out.println("No data in event[" + event + "]");
                } else {
                    System.out.println("Receive event: "
                            + "type=[" + event.getType() + "]"
                            + ", path=[" + data.getPath() + "]"
                            + ", data=[" + new String(data.getData()) + "]"
                            + ", stat=[" + data.getStat() + "]");
                }
            }
        });
        watcher.start();
        System.in.read();
        client.close();
    }
}
